const app = getApp()
import {
  queryProductsByComprehensive,
  queryProductsBySales,
  queryProductsByTime,
  queryProductsByPriceAsc,
  queryProductsByPriceDesc
} from "../../service/hunt"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 搜索框的值
    value: '',
    // 激活状态的Tab的Index
    activeTabIndex: 0,
    // 0 从高到低 1 从低到高
    priceStatus: 0,
    // 当前搜索页码
    page: 1,
    // 每页商品数量
    pageSize: 10,
    // 商品列表
    productList: [],
    // 下拉刷新状态
    isRefreshing: false,
    // 上拉刷新状态
    isLoading: false,
    // 为true时则没有更多数据了
    hasNoMore: false,
    // scroll-view 滚动距离
    scrollTop: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    console.log(options);
    this.setData({
      value: options.keyword
    })
    wx.showLoading({
      title: '加载中',
    })
    const res = await queryProductsByComprehensive(this.data.value, this.data.page, this.data.pageSize)
    wx.hideLoading()
    this.setData({
      productList: res.data.records
    })
  },
  /**
   * 点击Tab区域
   */
  async onTapTab(event) {
    const index = event.target.dataset.index
    // 点击后首先初始化搜索状态
    if (index === 2) {
      this.setData({
        activeTabIndex: index,
        priceStatus: this.data.priceStatus === 0 ? 1 : 0,
        page: 1,
        pageSize: 10
      })
    } else {
      this.setData({
        activeTabIndex: index,
        priceStatus: 0,
        page: 1,
        pageSize: 10
      })
    }
    if (index === 0) {
      // 综合搜索
      console.log("综合搜索");
      wx.showLoading({
        title: '加载中',
      })
      const res = await queryProductsByComprehensive(this.data.value, this.data.page, this.data.pageSize)
      console.log(res);
      wx.hideLoading()
      this.setData({
        productList: res.data.records
      })
    } else if (index === 1) {
      // 根据销量搜索
      console.log("根据销量搜索");
      wx.showLoading({
        title: '加载中',
      })
      const res = await queryProductsBySales(this.data.value, this.data.page, this.data.pageSize)
      console.log(res);
      wx.hideLoading()
      this.setData({
        productList: res.data.records
      })
    } else if (index === 2) {
      // 根据价格搜索
      if (this.data.priceStatus === 0) {
        // desc
        console.log("根据价格从高到底搜索");
        wx.showLoading({
          title: '加载中',
        })
        const res = await queryProductsByPriceDesc(this.data.value, this.data.page, this.data.pageSize)
        console.log(res);
        wx.hideLoading()
        this.setData({
          productList: res.data.records
        })
      } else {
        // desc
        console.log("根据价格从低到高搜索");
        wx.showLoading({
          title: '加载中',
        })
        const res = await queryProductsByPriceAsc(this.data.value, this.data.page, this.data.pageSize)
        console.log(res);
        wx.hideLoading()
        this.setData({
          productList: res.data.records
        })
      }
    } else {
      // 根据发布时间搜索
      console.log("根据发布时间搜索");
      wx.showLoading({
        title: '加载中',
      })
      const res = await queryProductsByTime(this.data.value, this.data.page, this.data.pageSize)
      console.log(res);
      wx.hideLoading()
      this.setData({
        productList: res.data.records
      })
    }
    this.setData({
      scrollTop: 0
    })

  },
  /**
   * 点击搜索栏，此时应该回到搜索页面
   */
  onTabSearchBox() {
    wx.navigateBack()
  },
  /**
   * scrollview下拉刷新
   */
  onRefresh() {
    console.log("下拉刷新");
  },
  /**
   * scrollview上拉刷新
   */
  async onReachBottom() {
    console.log("上拉刷新");
    this.data.page = this.data.page + 1
    const index = this.data.activeTabIndex
    this.setData({
      isLoading: true
    })
    if (index === 0) {
      // 综合搜索
      const res = await queryProductsByComprehensive(this.data.value, this.data.page, this.data.pageSize)
      console.log(res);
      if (res.data.records.length === 0) {
        this.setData({
          hasNoMore: true
        })
      }
      this.setData({
        productList: [...this.data.productList, ...res.data.records],
        isLoading: false
      })
    } else if (index === 1) {
      // 根据销量搜索
      const res = await queryProductsBySales(this.data.value, this.data.page, this.data.pageSize)
      console.log(res);
      if (res.data.records.length === 0) {
        this.setData({
          hasNoMore: true
        })
      }
      this.setData({
        productList: [...this.data.productList, ...res.data.records],
        isLoading: false
      })
    } else if (index === 2) {
      // 根据价格搜索
      if (this.data.priceStatus === 0) {
        // 从高到低
        const res = await queryProductsByPriceDesc(this.data.value, this.data.page, this.data.pageSize)
        console.log(res);
        if (res.data.records.length === 0) {
          this.setData({
            hasNoMore: true
          })
        }
        this.setData({
          productList: [...this.data.productList, ...res.data.records],
          isLoading: false
        })
      } else {
        // 从低到高
        const res = await queryProductsByPriceAsc(this.data.value, this.data.page, this.data.pageSize)
        console.log(res);
        if (res.data.records.length === 0) {
          this.setData({
            hasNoMore: true
          })
        }
        this.setData({
          productList: [...this.data.productList, ...res.data.records],
          isLoading: false
        })
      }
    } else {
      // 根据发布时间搜索
      const res = await queryProductsByTime(this.data.value, this.data.page, this.data.pageSize)
      console.log(res);
      if (res.data.records.length === 0) {
        this.setData({
          hasNoMore: true
        })
      }
      this.setData({
        productList: [...this.data.productList, ...res.data.records],
        isLoading: false
      })
    }
  },
  /**
   * 点击补货申请
   */
  onTapRestockApplication() {
    console.log("点击补货申请");
    wx.navigateTo({
      url: '/pages/Restock/Restock',
    })
  }

})